package com.css.dzj.dzjap.modules.expertcredit.controller;

import com.css.aq.server.service.BaseService;
import com.css.dzj.dzjap.common.util.BeanUtil;
import com.css.dzj.dzjap.common.util.Constant;
import com.css.dzj.dzjap.common.util.R;
import com.css.dzj.dzjap.modules.expertcredit.entity.ApExpertCreditEntity;
import com.css.dzj.dzjap.modules.expertcredit.service.ExpertCreditService;
import com.css.dzj.dzjap.modules.info.service.ExpertService;
import com.css.dzj.dzjap.modules.publick.entity.ApPublicEntity;
import com.css.dzj.dzjap.modules.publick.service.PublicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping(value = "/expertCred")
public class ExpertCreditController {

    @Autowired
    private ExpertCreditService expertService;

    @Autowired
    private PublicService publicService;

    @Autowired
    private ExpertService eService;
    @Autowired
    BaseService baseService;

    @GetMapping(value = "/getExpertCred")
    public R getExpertCred(@RequestParam("expertcreditId") String expertcreditId){
        baseService.logSuccess(3,"查询成功","2");
        return R.ok().put("data",expertService.getExpertCred(expertcreditId));
    }

      // 添加专家信用表

     @PostMapping(value = "/addExpertCred")
     public R addExpertCred(@RequestBody ApExpertCreditEntity exCred) {
        if (StringUtils.isEmpty(exCred.getExpertName()) || StringUtils.isEmpty(exCred.getExpertIdcard()) || StringUtils.isEmpty(exCred.getCertype())
                 || StringUtils.isEmpty(exCred.getExpertScore()) || StringUtils.isEmpty(exCred.getExpertLevl()) || exCred.getAssessmentDate() == null
                 || StringUtils.isEmpty(exCred.getAssessmentAuthority()) || StringUtils.isEmpty(exCred.getExpertCode())) {

             return R.error("必填字段为空");
         }
          try{

              String flag = expertService.addExpertCred(exCred);
              if (!"0".equals(flag)) {
                  R r = new R();
                  r.put("code", 200);
                  r.put("msg", "操作成功");
                  r.ok().put("data", flag);
                  baseService.logSuccess(3,"添加成功","2");
                  return r;
              } else {
                  baseService.logFailed(3,"添加失败","2");
                  return R.error("添加失败");
              }
          } catch (Exception e){
              baseService.logFailed(3,"添加失败","2");
              return R.error(e.getMessage());
          }



     }

    /**
     * 修改专家信用信息表
     */
    @PostMapping(value = "/updateExpertCred")
    public R updateExpertCred(@RequestBody ApExpertCreditEntity exCred ){

        try {
            ApExpertCreditEntity expertCred = expertService.getExpertCred(exCred.getExpertcreditId());
            BeanUtil.copyBeanNotNullToBean(exCred, expertCred);
            expertCred.setUpdateTime(new Date());
            expertService.update(expertCred);
            baseService.logSuccess(3,"修改成功","2");
            return R.ok();
        } catch (Exception e) {
            baseService.logFailed(3,"修改失败","2");
            return R.error(e.getMessage());
        }
    }

    /**
     * 删除专家信用信息表
     */
    @PostMapping(value = "/deleteExpertCred")
    public R deleteExpertCred(@RequestParam("expertcreditId") String expertcreditId){
        if(expertService.deleteExpertCred(expertcreditId)){
            baseService.logSuccess(3,"删除成功","2");
            return R.ok("操作成功!");
        }else{
            baseService.logFailed(3,"删除失败","2");
            return  R.error("操作失败!");
        }
    }


    /**
     *移入专家信用信息表进入黑名单
     */
    @PostMapping(value = "/moveinExpertCred")
    public R moveinExpertCred(@RequestBody  ApExpertCreditEntity exCred){
        if(StringUtils.isEmpty(exCred.getSpecause())){
            return R.error("必填字段不能为空！");
        }
        try {
            ApPublicEntity pub = publicService.getGlPublick(exCred.getExpertcreditId());
            if(pub!=null){
                ApPublicEntity aPub = new ApPublicEntity();
                pub.setPublicUrl("/dzjap/expertCred/getReExpertCred");
                BeanUtil.copyBeanNotNullToBean(aPub, pub);
                publicService.update(aPub);
            }
            eService.updateBlack("1",exCred.getExpertIdcard());
            ApExpertCreditEntity expertCred = expertService.getExpertCred(exCred.getExpertcreditId());
            BeanUtil.copyBeanNotNullToBean(exCred, expertCred);
            expertCred.setExcpresState(Constant.EXCPRES_STATE_Yes);
            expertCred.setAbnormalPutdate(new Date());
            expertService.update(expertCred);
            baseService.logSuccess(3,"移入成功","2");
            return R.ok();
        } catch (Exception e) {
            baseService.logSuccess(3,"移入失败","2");
            return R.error(e.getMessage());
        }
    }

    /**
     *移出专家信用信息表进入黑名单
     */
    @PostMapping(value = "/moveoutExpertCred")
    public R moveoutExpertCred(@RequestBody  ApExpertCreditEntity exCred){

        if(StringUtils.isEmpty(exCred.getRemexcpres())){
            return R.error("必填字段不能为空！");
        }
        try {
            ApPublicEntity pub = publicService.getGlPublick(exCred.getExpertcreditId());
            if(pub!=null){
                ApPublicEntity aPub = new ApPublicEntity();
                pub.setPublicUrl("/dzjap/expertCred/getExpertCred");
                BeanUtil.copyBeanNotNullToBean(aPub, pub);
                publicService.update(aPub);
            }

            eService.updateBlack("0",exCred.getExpertIdcard());
            ApExpertCreditEntity expertCred = expertService.getReExpertCred(exCred.getExpertcreditId());
            BeanUtil.copyBeanNotNullToBean(exCred, expertCred);
            expertCred.setExcpresState(Constant.EXCPRES_STATE_NO);
            expertCred.setRemdate(new Date());
            expertService.update(expertCred);
            baseService.logSuccess(3,"移出成功","2");
            return R.ok();
        } catch (Exception e) {
            baseService.logFailed(3,"移出失败","2");
            return R.error(e.getMessage());
        }

    }

    /**
     * 查询专家信用信息表黑名单
     */
    @GetMapping(value = "/getReExpertCred")
    public R getReExpertCred(@RequestParam("expertcreditId") String expertcreditId){
        baseService.logSuccess(3,"查询成功","2");
        return R.ok().put("data",expertService.getReExpertCred(expertcreditId));
    }

    /**
     *分页查询所有专家信用信息表
     */
    @GetMapping(value = "/getAllExpertCred")
    public R getAllExpertCred(@RequestParam(required = false) Integer pageNo,
                              @RequestParam(required = false) Integer pageSize
                              ) {

        PageRequest pageRequest = PageRequest.of(pageNo-1, pageSize);
        baseService.logSuccess(3,"查询成功","2");
        return R.ok().put("data", expertService.getAllExpertCred(pageRequest));
    }

    /**
     *分页查询所有专家信用信息表黑名单
     */
    @GetMapping(value = "/getAllReExpertCred")
    public R getAllReExpertCred(@RequestParam(required = false) Integer pageNo,
                                @RequestParam(required = false) Integer pageSize
                                ) {

        PageRequest pageRequest = PageRequest.of(pageNo-1, pageSize);
        baseService.logSuccess(3,"查询成功","2");
        return R.ok().put("data", expertService.getAllReExpertCred(pageRequest));
    }

    /**
     *多条件分页查询所有专家信用信息表
     */
    @GetMapping(value ="/getAllConExpertCred")
    public R getAllConExpertCred(@RequestParam String expertName,
                                 @RequestParam(required = false) String expertIdcard,
                                 @RequestParam(required = false) String certype,
                                 @RequestParam(required = false) String expertCode,
                                 @RequestParam(required = false) String expertScore,
                                 @RequestParam(required = false) String expertLevl,
                                 @RequestParam(required = false) String expertClassification,
                                 @RequestParam(required = false) Integer pageNo,
                                 @RequestParam(required = false) Integer pageSize){

        PageRequest pageRequest = PageRequest.of(pageNo-1,pageSize);
        /*Timestamp begin = (Timestamp)beginTime;
        Timestamp end = (Timestamp)endTime;*/
        baseService.logSuccess(3,"查询成功","2");
        return R.ok().put("data", expertService.getAllConExpertCred(pageRequest,expertName,expertIdcard,certype,expertCode,expertScore,expertLevl,expertClassification));
    }


    /**
     *多条件分页查询所有专家信用信息表黑名单
     */
    @GetMapping(value ="/getAllReConExpertCred")
    public R getAllReConExpertCred(@RequestParam(required = false) String expertName,
                                   @RequestParam(required = false) String expertIdcard,
                                   @RequestParam(required = false) String certype,
                                   @RequestParam(required = false) String expertCode,
                                   @RequestParam(required = false) String expertScore,
                                   @RequestParam(required = false) String expertLevl,
                                   @RequestParam(required = false) String expertClassification,
                                   @RequestParam(required = false) Integer pageNo,
                                   @RequestParam(required = false) Integer pageSize){

        PageRequest pageRequest = PageRequest.of(pageNo-1,pageSize);
        /*Timestamp begin = (Timestamp)beginTime;
        Timestamp end = (Timestamp)endTime;*/
        baseService.logSuccess(3,"查询成功","2");
        return R.ok().put("data", expertService.getAllReConExpertCred(pageRequest,expertName,expertIdcard,certype,expertCode,expertScore,expertLevl,expertClassification));
    }


    /**
     * 批量删除
     */
    @PostMapping(value = "/deleteExpertCredList")
    public R deleteExpertCredList(@RequestBody String[] expertcreditIds){
        System.out.println(expertcreditIds);
        if(expertcreditIds.length<0 || expertcreditIds==null){

            return R.error("操作失败！");
        }
        List<ApExpertCreditEntity> list = new ArrayList();

        for (String expertcreditId : expertcreditIds){

            ApExpertCreditEntity exCred =  expertService.getExpertCred(expertcreditId);

            if(exCred!=null){
                exCred.setDelFlag(Constant.DELETE);//逻辑删除
                exCred.setUpdateTime(new Date());
                list.add(exCred);
            }else {
                return R.error("操作失败！");
            }
        }
            if(list!=null && list.size()>0){
                expertService.update(list);
            }else{
                return R.error("操作失败！");
            }
            baseService.logSuccess(3,"删除成功","2");
            return R.ok();

    }


    //导入专家信用信息表
   /* @RequestMapping(value = "/expertImport")
    public R expertImport(@RequestParam MultipartFile file){

        String fileName = file.getOriginalFilename();

        try{
            String flag = expertService.expertImport(fileName,file);
            if (!"0".equals(flag)) {
                R r = new R();
                r.put("code", 200);
                r.put("msg", "操作成功");
                r.ok().put("data", flag);
                return r;
            } else {
                return R.error("添加失败");
            }
        } catch (Exception e){
            return R.error(e.getMessage());
        }
    }*/

}
